package com.koron.order.experiment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.experiment.bean.po.ExperimentItemPo;
import com.koron.order.experiment.bean.vo.ExperimentItemVo;
import com.koron.order.experiment.mapper.ExperimentItemMapper;
import com.koron.order.experiment.service.ExperimentItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

/**
 * (ExperimentItem)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-08 17:33:33
 */
@Service("experimentItemService")
public class ExperimentItemServiceImpl implements ExperimentItemService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, ExperimentItemPo experimentItem) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        if (StringUtils.isEmpty(experimentItem.getId())) {
            experimentItem.setId(CodeTools.getCode32());
        }

        experimentItem.setCreateBy(getCurrUser().getAccount());
        experimentItem.setCreateByName(getCurrUser().getName());

        return mapper.insertOrUpdate(experimentItem);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<ExperimentItemPo> experimentItemList) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        if (CollectionUtils.isEmpty(experimentItemList)) {
            return 0;
        }
        experimentItemList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
            p.setCreateBy(getCurrUser().getAccount());
            p.setCreateByName(getCurrUser().getName());
        });

        return mapper.insertOrUpdateBatch(experimentItemList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentItemPo experimentItem) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        if (StringUtils.isEmpty(experimentItem.getId())) {
            experimentItem.setId(CodeTools.getCode32());
        }

        experimentItem.setCreateBy(getCurrUser().getAccount());
        experimentItem.setCreateByName(getCurrUser().getName());

        return mapper.insert(experimentItem);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentItemPo> experimentItems) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        if (CollectionUtils.isEmpty(experimentItems)) {
            return 0;
        }
        experimentItems.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
            p.setCreateBy(getCurrUser().getAccount());
            p.setCreateByName(getCurrUser().getName());
        });
        //先根据试验工单id删除
        mapper.deleteByBusinessId(experimentItems.get(0).getExperimentId(), "", "");
        return mapper.insertBatch(experimentItems);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentItemPo experimentItem) {

        Assert.hasText(experimentItem.getId(), "主键id不能为空");
        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);
        experimentItem.setUpdateBy(getCurrUser().getAccount());
        experimentItem.setUpdateByName(getCurrUser().getName());

        return mapper.update(experimentItem);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentItemPo queryById(SessionFactory factory, String id) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentItemPo> queryList(SessionFactory factory, ExperimentItemPo experimentItem) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        List<ExperimentItemPo> list = mapper.queryList(experimentItem);

        return list;
    }

    @Override
    public List<ExperimentItemVo> queryByEquipmentId(SessionFactory factory, String orderId, String experimentEquipId) {

        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        List<ExperimentItemVo> itemVos = mapper.queryByEquipmentId(orderId, experimentEquipId);

        if (CollectionUtils.isNotEmpty(itemVos)) {
            Map<String, ExperimentItemVo> map = new HashMap<>();
            for (ExperimentItemVo itemVo : itemVos) {
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                itemVo.setExperimentItemVoList(new ArrayList<>());
                if (StringUtils.isEmpty(parentId)) {
                    map.put(id, itemVo);
                }
            }

            Iterator<ExperimentItemVo> iterator = itemVos.iterator();

            while (iterator.hasNext()) {
                ExperimentItemVo itemVo = iterator.next();
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                if (StringUtils.isNotEmpty(parentId)) {
                    ExperimentItemVo experimentItemVo = map.get(parentId);
                    if (!Objects.isNull(experimentItemVo)) {
                        //找到了父节点,应该从原来列表删除
                        iterator.remove();
                        List<ExperimentItemVo> experimentItemVoList = experimentItemVo.getExperimentItemVoList();
                        experimentItemVoList.add(itemVo);
                    }
                }
            }
        }

        return itemVos;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ExperimentItemPo experimentItem, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        ExperimentItemMapper mapper = factory.getMapper(ExperimentItemMapper.class);

        List<ExperimentItemPo> list = mapper.queryList(experimentItem);

        return new PageInfo(list);
    }

}